package com.googlecode.gwt4gae.client;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TabPanelEvent;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.google.gwt.user.client.History;
import com.googlecode.gwt4gae.client.menu.MenuModel;

public abstract class CententView extends BaseView {
	protected TabItem tabItem = new TabItem();

	public CententView(BaseAction baseAction) {
		super(baseAction);
		tabItem.setClosable(true);
		tabItem.setId(baseAction.getClass().getName());
	}

	@Override
	protected void initialize() {
		initContentPanel();
		createContent(this.tabItem);
	}
	
	protected abstract void createContent(TabItem parent);

	protected TabPanel initContentPanel() {
		TabPanel tabPanel = Registry.get(Gwt4gaeConstants.CONTENT_PANEL);
		if (tabPanel == null) {
			tabPanel = new TabPanel();
			tabPanel.setCloseContextMenu(true);
			tabPanel.setBorderStyle(false);
			tabPanel.setBodyBorder(false);
			tabPanel.setTabScroll(true);
			tabPanel.setAnimScroll(true);

			tabPanel.addListener(Events.Remove, new Listener<TabPanelEvent>() {
				public void handleEvent(TabPanelEvent be) {
					TabItem item = be.getItem();
					if (item.getId().equals(
							Gwt4gaeConstants.WELCOME_ACTION_NAME)) {
						return;
					}
					MenuModel menuModel = (MenuModel) item.getData("menuModel");
					Dispatcher.forwardEvent(AppEvents.HIDE_PAGE, menuModel);
				}

			});
			tabPanel.addListener(Events.Select, new Listener<TabPanelEvent>() {
				public void handleEvent(TabPanelEvent be) {
					TabItem item = be.getItem();
					if (item.getId().equals(
							Gwt4gaeConstants.WELCOME_ACTION_NAME)) {
						return;
					}
					String token = History.getToken();
					MenuModel menuModel = (MenuModel) item.getData("menuModel");
					if (token != null && (!token.equals(menuModel.getUrl()))) {
						History.newItem(menuModel.getUrl(), false);
					}
					Dispatcher.forwardEvent(AppEvents.TAB_CHANGE, menuModel);
				}
			});

			tabPanel.syncSize();
			ContentPanel centerPanel = Registry
					.get(Gwt4gaeConstants.CENTER_PANEL);
			centerPanel.add(tabPanel);
			centerPanel.syncSize();

			Registry.register(Gwt4gaeConstants.CONTENT_PANEL, tabPanel);
		}

		return tabPanel;
	}

	@Override
	protected void handleEvent(AppEvent event) {
		TabPanel contentPanel = Registry.get(Gwt4gaeConstants.CONTENT_PANEL);
		MenuModel menuModel = event.getData();
		if (menuModel == null) {
			contentPanel.add(tabItem);
			return;
		}
		TabItem item = contentPanel.findItem(menuModel.getUrl(), false);
		if (item == null) {
			tabItem.setData("menuModel", menuModel);
			tabItem.setId(menuModel.getUrl());
			contentPanel.add(tabItem);
			item = tabItem;
		}

		if (item != contentPanel.getSelectedItem()) {
			contentPanel.setSelection(item);
		}
	}

}
